<?php

namespace App\Http\Controllers\Guard\Rbac;

use App\Http\Controllers\Common\GuardController;
use App\Models\Constant\Constant;
use App\Models\Rbac\GuardUser;
use Illuminate\Http\Request;

class GuardUserController extends GuardController {

    public function lst(Request $request) {
        $name   = $request->input('name');
        $phone  = $request->input('phone');
        $page  = $request->input('page',1);
        $limit  = $request->input('limit',Constant::PAGE_SIZE);
        $conditions = compact('name','phone');
        $records = (new GuardUser())->getLst($conditions)->paginate($limit,['*'],'page',$page);
        return responseSuccess($records->items(),$records->total());
        //return responseSuccess($records->items(),$records->total(),100000,[],true,[],20000);
    }

    public function add(Request $request) {
        $info = $request->input();
        $id = $request->input('id',0);
        $name = $request->input('name');
        $this->validate($request, [
            'name'=>'required',
            'selectRoleIds'=>'required'
        ],[],[
            'name'=>'用户名',
            'selectRoleIds'=>'角色'
        ]);

        $info['role_ids'] = implode(',', $request->input('selectRoleIds',[]));
        try{
            //id小于等于0，说明是新增操作
            if($id<=0){
                //检查用户名是否存在
                if(!empty((new GuardUser())->getUserByName($name)))
                    return responseFail(300000);
                $info['password'] = md5($info['password']);
            }
            //如果是修改操作
            else{
                unset($info['password']);
            }
            unset($info['selectRoleIds']);
            unset($info['repassword']);
            GuardUser::updateOrCreate(compact('id'),$info);
            return responseSuccess();
        }catch(\Exception $e){
            $msg = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            return responseFail($msg);
        }
    }

    public function edit(Request $request){
        $info = $request->input();
        $this->validate($request, [
            'id'=>'required'
        ]);
        $id = $info['id'];

        if(isset($info['selectPermissionIds'])){
            $info['permission_ids'] = implode(',', $info['selectPermissionIds']);
            unset($info['selectPermissionIds']);
        }
        GuardUser::updateOrCreate(compact('id'),$info);
        return responseSuccess();
    }

    public function resetPassword(Request $request) {
        $oldPassword = $request->input('oldPassword');
        $password = $request->input('password');
        $token = $request->header('token');
        $id = checkJWT($token)['id'];

        $user = (new GuardUser())->getGuardUserById($id);
        if(empty($user)){
            return responseFail('无效用户');
        }
        if($user->password!=md5($oldPassword)){
            return responseFail('原密码错误！');
        }
        $user->password = md5($password);
        $user->save();
        return responseSuccess();
    }

    public function coverPassword(Request $request) {
        $id = $request->input('id',0);
        $password = 'project@123';
        GuardUser::where(compact('id'))->update(['password'=>md5($password)]);
        return responseSuccess([],0,'重置成功，新密码为：'.$password);
    }

    public function getUserByToken(Request $request){
        $token = $request->header('token');
        $id = checkJWT($token)['id'];
        return $this->getRecordByIds(GuardUser::class, $id);
    }


    public function getRecord(Request $request) {
        return $this->getRecordByIds(GuardUser::class, $request->input('id','')); // TODO: Change the autogenerated stub
    }

    public function del(Request $request) {
        return $this->delRecords(GuardUser::class, $request->input('id',''));
    }

    public function getPermission(Request $request) {
        $token = $request->header('token');
        $id = checkJWT($token)['id'];
        $guardUser = (new GuardUser())->getGuardUserById($id);
        $records =  $guardUser->hasPermissions();
        return responseSuccess($records->toArray());
    }


}
